package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.TeacherMapper;
import com.example.pojo.careerTalk;
import com.example.dto.result;
import com.example.pojo.tracking;
import com.example.pojo.user;
import com.example.service.TeacherService;
import com.example.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherMapper teacherMapper;

    //用户信息
    @GetMapping("/list")
    public result findAll(@RequestParam Integer page, @RequestParam Integer size){
        Page<user> userPage = new Page<>(page,size);
        IPage<user> res = teacherService.findAll(userPage);
        return result.success(res);
    }

    //就业信息
    @GetMapping("/careerList")
    public result findAllCareerList(@RequestParam Integer page , @RequestParam Integer size){
        Page<tracking> trackingPage = new Page<>(page,size);
        IPage<tracking> res = teacherService.findAllCareerList(trackingPage);
        return result.success(res);
    }

    //宣讲会信息
    @GetMapping("/talkList")
    public result findAllTalkList(@RequestParam Integer page, @RequestParam Integer size){
        Page<careerTalk> careerTalkPage = new Page<>(page,size);
        IPage<careerTalk> res = teacherService.findAllTalkList(careerTalkPage);
        return result.success(res);
    }

    //查询用户信息(学号)
    @GetMapping("/findStudentById")
    public result findStudentById(@RequestParam String key){
        if(key!=null){
            Page<user> userPage = new Page<>(1,5);
            IPage<user> res = teacherService.findStudentById(key,userPage);
           if(res==null){
               return result.error("没有找到这个人");
           }
           else {
               return result.success(res);
           }
        }
        return result.error("查找失败");
    }

    //查询用户信息(姓名)
    @GetMapping("/findStudentByName")
    public result findStudentByName(@RequestParam String key){
        if(key!=null)
        {
            Page<user> userPage = new Page<>(1,5);
            IPage<user> res = teacherService.findStudentByName(key,userPage);
            if(res==null){
                return result.error("没有找到这个人");
            }
            else {
                return result.success(res);
            }
        }

        return result.error("查找失败");
    }

    //管理员端新增用户信息
    @PostMapping("/addUser")
    public result addUser(@RequestBody user u){
        user temp = teacherMapper.findStudentById(u.getName());
        System.out.print(u);
        if(temp == null){
            //MD5加密
            String s = Md5Utils.getMd5String(u.getPassword());
            u.setPassword(s);
            teacherService.addUser(u);
            return result.success("添加成功!");
        }
        return result.error("用户已存在!");
    }

    //删除用户信息
    @DeleteMapping("/deleteUser")
    public result deleteUser(@RequestParam String id){
        if(id == null){
            return result.error("删除失败");
        }
        teacherService.deleteUser(id);
        return result.success("删除成功");
    }

}
